﻿using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using ControleRepresentante.Data.Catalogs;
using ControleRepresentante.Logic;
using ControleRepresentante.Logic.ImpostoHandlers;
using ControleRepresentante.Logic.Models;
using ControleRepresentante.Logic.Repositories;
using WebSite.Controllers;
using WebSite.Controllers.ControllerExtensions;
using WebSite.Controllers.PrintTemplates;
using HandleErrorAttribute = System.Web.Mvc.HandleErrorAttribute;

namespace WebSite
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            ControleRepresentanteDependencyResolver controleRepresentanteDependencyResolver = new ControleRepresentanteDependencyResolver();
            RegisterServices(controleRepresentanteDependencyResolver);
            DependencyResolver.SetResolver(controleRepresentanteDependencyResolver);
        }

        private static void RegisterServices(ControleRepresentanteDependencyResolver controleRepresentanteDependencyResolver)
        {
            AreaOperacaoRegistry areaOperacaoRegistry = new AreaOperacaoRegistry();
            AreaOperacaoRegistry.RegisterControleRepresentanteAreas(areaOperacaoRegistry);
            controleRepresentanteDependencyResolver.Bind<IAreaOperacaoRegistry>().ToConstant(areaOperacaoRegistry);
            controleRepresentanteDependencyResolver.Bind<ISessionConfiguration>().To<SessionConfiguration>();
            controleRepresentanteDependencyResolver.Bind<IControllerExtensionProvider>().To<ControllerExtensionProvider>();
            controleRepresentanteDependencyResolver.Bind<IPrintTemplateSelector>().To<PrintTemplateSelector>();
            controleRepresentanteDependencyResolver.Bind<IImpostoProvider>().To<ImpostoProvider>();

            controleRepresentanteDependencyResolver.Bind<IRepositoryProvider>().ToMethod(() =>
            {
                RepositoryProvider<ControleRepresentanteCatalog> repositoryProvider = HttpContext.Current.Session["RepositoryProvider"] as RepositoryProvider<ControleRepresentanteCatalog>;
                if (repositoryProvider == null)
                {
                    RepositoryProviderConfiguration repositoryProviderConfiguration = new RepositoryProviderConfiguration(
                        () => HttpContext.Current.Session["UserInfo"] as UserInfo,
                        () => HttpContext.Current.Session["RepresentanteId"] as Guid?);

                    repositoryProvider = new RepositoryProvider<ControleRepresentanteCatalog>(repositoryProviderConfiguration);
                    RepositoryProvider.RegisterControleRepresentanteRepositories(repositoryProvider);
                    HttpContext.Current.Session["RepositoryProvider"] = repositoryProvider;
                }
                return repositoryProvider;
            });
        }
    }
}